﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using NHibernate.Cfg;

namespace BasicFrameworkDemo.Utility
{
    using FluentNHibernate.Cfg;
    using FluentNHibernate.Cfg.Db;

    using NHibernate;
    using NHibernate.ByteCode.Castle;
    using NHibernate.Context;
    using NHibernate.Tool.hbm2ddl;

    public class FluentNHibernateUtility : NHibernateUtilityBase
    {
        private static FluentNHibernateUtility fluentNHibernateUtilityInstance ;

        private FluentNHibernateUtility()
        {
        }

        public static FluentNHibernateUtility GetInstance()
        {
            return fluentNHibernateUtilityInstance?? (fluentNHibernateUtilityInstance = new FluentNHibernateUtility());
        }
        /// <summary>
        /// Ns the config.
        /// </summary>
        /// <returns></returns>
        protected override Configuration NConfig()
        {
            var nConfig =
                Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008.ConnectionString(
                        str => str.FromConnectionStringWithKey("connectionStrings"))
                        .AdoNetBatchSize(100)
                    )
                    .ProxyFactoryFactory<ProxyFactoryFactory>()
                    .Mappings(
                        x => x.FluentMappings.AddFromAssembly(Assembly.Load(SystemConfig.FluentNHibernateAssembly)))
                    .CurrentSessionContext("web")
                    .BuildConfiguration()
                    .SetProperty("hbm2ddl.auto", "update");
               
            return nConfig;
        }
    }
}
